//
//  DBStatuses.swift
//  大波微博swift
//
//  Created by 大波 on 16/3/3.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

class DBStatuses: NSObject {

    /// 微博创建时间
    var created_at:	String? {
        
        didSet {
            
            created_at = NSDate.sinaDateStringToDate(created_at!).dateDescription()
        }
    }
    
    /// 微博ID
    var id:	Int = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 被转发的原微博信息字段，当该微博为转发微博时返回
    var retweeted_status: DBStatuses?
    
    /// 微博来源
    var source: String? {
        
        didSet {
            
            source = source?.sinaSource()
        }
    }
    
    /// 微博作者的用户信息字段
    var user: DBUserStatuse?
    
    /// 图片数组
    var pic_urls: [[String : AnyObject]]? {
        
        didSet {
            
            let count = pic_urls?.count ?? 0
            
            if count == 0 {
                
                return
            }
            
            var picArr = [NSURL]()
            
            for dic in pic_urls! {
                
                let url = NSURL(string: dic["thumbnail_pic"] as! String)
                
                picArr.append(url!)
            }
            
            picDataURL = picArr
        }
    }
    
    /// 图片数组URL
    var picDataURL: [NSURL]?
    
    init(dic: [String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dic)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
//        print("key: \(key)")
        
        if key == "user" {
            
            if let dic = value as? [String : AnyObject] {
                
                user = DBUserStatuse(dic: dic)
            }
            
            return
            
        } else if key == "retweeted_status" {
            
            if let dict = value as? [String: AnyObject] {
                
                // 字典转模型
                retweeted_status = DBStatuses(dic: dict)
            }
            
            // 要return,不要走父类的将字典赋值给retweeted_status属性
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    override var description: String {
        
        let keys = ["created_at", "text", "source", "id", "pic_urls", "retweeted_status"]
        
        return "\n\t DBStatuses模型数据: \(dictionaryWithValuesForKeys(keys).description)"
    }
}
